package com.example.restaurant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.restaurant.common.Log;
import com.example.restaurant.common.RestaurantException;
import com.example.restaurant.common.allenum.DishStatus;
import com.example.restaurant.model.Dish;
import com.example.restaurant.dao.DishMapper;
import com.example.restaurant.model.DishType;
import com.example.restaurant.service.DishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.restaurant.service.dto.FeiDishDto;
import com.example.restaurant.service.dto.SelDishByStoreIdAndName;
import com.example.restaurant.service.dto.SelDishTypeByUserId;
import com.example.restaurant.service.dto.SelStoreByUidDto;
import com.example.restaurant.service.param.AddDishsParam;
import com.example.restaurant.service.param.UpdateDishByIdParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * <p>
 * 菜品表 服务实现类
 * </p>
 *
 * @author x
 * @since 2022-09-29
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Resource
    private DishMapper dishMapper;

    @Override

    //新增菜品
    @Log
    @Transactional
    public boolean addDish(AddDishsParam addDishParam) {
        log.info("{}",addDishParam.toString());
        Dish dish = BeanUtil.copyProperties(addDishParam, Dish.class);
        dish.setStatus(DishStatus.TAKE_DOWN.toString());
        dish.setQuantity(0);
        int insert = dishMapper.insert(dish);
        if(insert<1){
            throw new RestaurantException("新增菜品失败");
        }
        return true;
    }

    @Override
    @Log
    //查询菜品 根据用户id
    public IPage<FeiDishDto> getDish(Integer userId, int current, int size){
        log.info("{},{},{}",userId,current,size);
        String s = String.valueOf(userId);
        log.info("{}",s);
        if(s==null||userId<1){
            throw new RestaurantException("参数错误");
        }
        Page<FeiDishDto> dishParamPage = new Page<>(current,size);
        IPage<FeiDishDto> dish = dishMapper.getDish(dishParamPage, userId);
        if(dish.getTotal()<=0){
            throw new RestaurantException("没有查询的数据");
        }
        return dish;
    }

    @Override
    @Log
    @Transactional
    //修改菜品
    public boolean updateDish(UpdateDishByIdParam updateDishByIdParam) {
        log.info("{}",updateDishByIdParam.toString());
        Dish dish = BeanUtil.copyProperties(updateDishByIdParam, Dish.class);
        QueryWrapper<Dish> dishQueryWrapper = new QueryWrapper<>();
        dishQueryWrapper.eq("id",updateDishByIdParam.getId());
        Dish dish1 = dishMapper.selectById(updateDishByIdParam.getId());
        if(dish1==null){
            throw new RestaurantException("没有该菜品");
        }

        int update = dishMapper.update(dish, dishQueryWrapper);
        if(update<1){
            throw new RestaurantException("菜品修改失败");
        }
        return true;
    }

    @Override
    @Log
    @Transactional
    //删除菜品，根据菜品id
    public boolean deleteDish(int dishId) {
        if(dishId<1){
            throw new RestaurantException("参数校验失败");
        }
        int i = dishMapper.deleteById(dishId);
        if(i<1){
            throw new RestaurantException("删除失败");
        }
        return true;

    }

    @Override
    //预加载新增菜品的下拉框，门店
    public List<SelStoreByUidDto> selStoreByUid(int userId) {

        List<SelStoreByUidDto> selStoreByUids = dishMapper.selStoreByUid(userId);
        if(selStoreByUids.size()==0){
            throw new RestaurantException("旗下没有没有门店");
        }
        return selStoreByUids;
    }

    @Override
    public List<SelDishTypeByUserId> selDishTypeByUserId(int userId) {
        List<SelDishTypeByUserId> selDishTypeByUserIds = dishMapper.selDishTypeByUserId(userId);
        if(selDishTypeByUserIds.size()==0){
            throw new RestaurantException("旗下没有菜品分类");
        }
        return selDishTypeByUserIds;
    }

    @Override
    public Page<Dish> selDishByStoreIdAndName(int storeId,String dishName,int current,int size) {
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id",storeId);
        List<Dish> dish = dishMapper.selectList(queryWrapper);
        if(dish.size()==0){
            throw new RestaurantException("该店还没有菜品，请去添加");
        }
        queryWrapper.like("dish_name",dishName);
        Page<Dish> dishPage = dishMapper.selectPage(new Page<>(current, size), queryWrapper);

        return dishPage;
    }

    //通过菜类Id获取菜品
    @Override
    public List<Dish> getDishByDishTypeId(int dishTypeId) {
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dish_type_id",dishTypeId);
        List<Dish> dishes = dishMapper.selectList(queryWrapper);
        if(dishes.size()==0){
            throw new RestaurantException("该类下还没有菜品，请联系商家添加");
        }else{
            return dishes;
        }
    }

    @Override
    public List<Dish> getAllDishByType(int storeId, int dishTypeId) {
        QueryWrapper<Dish> dishQueryWrapper = new QueryWrapper<>();
        dishQueryWrapper.eq("store_id",storeId);
        dishQueryWrapper.eq("dish_type_id",dishTypeId);
        dishQueryWrapper.eq("status","USE");
        List<Dish> dishes = dishMapper.selectList(dishQueryWrapper);
        if (dishes.size()==0){
            throw new RestaurantException("该类下还没有菜品");
        }
        return dishes;
    }

    @Override
    public List<Dish> getAllDishByName(int storeId, String dishName) {
        QueryWrapper<Dish> dishQueryWrapper = new QueryWrapper<>();
        dishQueryWrapper.eq("store_id",storeId);
        dishQueryWrapper.like("dish_name",dishName);
        dishQueryWrapper.eq("status","USE");
        List<Dish> dishes = dishMapper.selectList(dishQueryWrapper);
        if (dishes.size()==0){
            throw new RestaurantException("没有该菜品");
        }
        return dishes;
    }

    @Override
    public List<Dish> getAllDish(int storeId) {
        QueryWrapper<Dish> dishQueryWrapper = new QueryWrapper<>();
        dishQueryWrapper.eq("store_id",storeId);
        dishQueryWrapper.eq("status","USE");
        List<Dish> dishes = dishMapper.selectList(dishQueryWrapper);
        if (dishes.size()==0){
            throw new RestaurantException("还没有菜品");
        }
        return dishes;
    }

    @Override
    //根据门店id和模糊菜品名,模糊查询菜品
    public IPage<FeiDishDto> getDishByStoreAndName(int storeId, String dishName, int current, int size) {
        Page<FeiDishDto> dishParamPage = new Page<>(current,size);
        String dishName1 = "%" + dishName + "%";
        IPage<FeiDishDto> dish = dishMapper.getDishByStoreAndName(dishParamPage, storeId,dishName1);
        if(dish.getTotal()<=0){
            throw new RestaurantException("没有查询的数据");
        }
        return dish;
    }
}
